import datetime
import logging

loggers = {}
LOG_PATH = '..\\{}.log'
LOG_LEVEL = 'DEBUG'
LOG_FORMAT = '%(levelname)s,%(asctime)s,%(filename)s,%(lineno)s：[%(message)s]'


def get_logger(name=None):
    global loggers
    if not name:
        name = __name__
    if loggers.get(name):
        return loggers.get(name)
    logger = logging.getLogger(name)
    logger.setLevel(LOG_LEVEL)
    now = datetime.datetime.now()
    get_time = now.strftime('%Y-%m-%d')
    file_handler = logging.FileHandler(LOG_PATH.format(get_time), encoding='utf-8')
    file_handler.setLevel(level=LOG_LEVEL)
    formatter = logging.Formatter(LOG_FORMAT, datefmt="%H:%M:%S")
    file_handler.setFormatter(formatter)
    logger.addHandler(file_handler)
    loggers[name] = logger
    return logger
